package com.sf.osms.dep.dao.impl;

import java.util.Date;
import java.util.List;

import org.dozer.Mapper;
import org.hibernate.criterion.Conjunction;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Disjunction;
import org.hibernate.criterion.Restrictions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.sf.novatar.base.dao.BaseBeanDao;
import com.sf.osms.dep.dao.IHandoverDao;
import com.sf.osms.dep.domain.Handover;
/**
 * handover dao impl
 * @author 841245
 *
 */
@Repository
public class HandoverDaoImpl extends BaseBeanDao<Long,Handover>  implements IHandoverDao {

	private Logger logger = LoggerFactory.getLogger(this.getClass());
	
	//根据ID查找的SQL
	private static final String DETAIL_SQL;
	
	static {
		DETAIL_SQL=" SELECT	h.FILE_NAME AS fileName,	u.username AS userName,	h.CREATE_TIME AS createTime,d.td_value AS countryName,a. NAME AS agentName, "
		+" h.network AS network,	h.SF_WAYBILL AS sfWaybill,	h.AGENT_WAYBILL AS agentWaybill,	h.path,	h.match_date as matchDate, "
		+" h.receiver_address as receiverAddress,	h.reciever_postal_code as recieverPostalCode,	h.receiver, "
		+" h.receiver_phone as receiverPhone,	h.freight_Term as freightTerm,	h.piece,	h.weight,	h.shipment_type as shipmentType, "
		+" h.freight_amount as freightAmount,	h.tax_Duties as taxDuties,	h.total ,h.modify_date as modifyDate,u2.username AS modifier FROM wbs_tt_handover h "
		+" INNER JOIN tt_datadict d ON h.COUNTRY_CODE = d.td_key LEFT JOIN ts_user u ON h.USER_ID = u.USER_ID left JOIN ts_user u2 on h.modifier_ID = u2.USER_ID LEFT JOIN wbs_tt_agent a ON h.AGENT_ID = a.id "
	    +" WHERE 	d. NAME = 'd_country' AND d.locale =:lang "; 
	}
	
	@Autowired
	private Mapper mapper;

	
	@Override
	public List<Handover> queryWithinTime(Date startDate) {
		//创建时间> 5天前 OR 修改时间 > 5天前
		DetachedCriteria criteria = DetachedCriteria.forClass(Handover.class);
		Disjunction or=Restrictions.disjunction();  
		or.add(Restrictions.gt("createTime", startDate));
		
		Conjunction and = Restrictions.conjunction();
		and.add(Restrictions.isNotNull("modifyDate"));
		and.add(Restrictions.gt("modifyDate", startDate));
		
		or.add(and);
		criteria.add(or);
		List<Handover> result = this.findBy(criteria);
		return result;
	}


	

}
